
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IClientBO;
import com.wyzz.hr.bo.RoleBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.dao.RoleDao;
import com.wyzz.hr.pojo.Authority;
import com.wyzz.hr.pojo.Role;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.StringUtil;

public class RoleBoImpl implements RoleBo {

	public RoleBoImpl() {
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public Role getRole(String roleId) {
		return (Role) roleDao.loadObject(com.wyzz.hr.pojo.Role.class, roleId,
				null, new boolean[0]);
	}

	public boolean isHasAuth(String empNo, Integer moduleId) {
		UserBo userBo = (UserBo) SpringBeanFactory.getBean("userService");
		Userinfo user = userBo.getUserById(empNo);
		if (user == null)
			return false;
		List auths = user.getUiAuthList();
		if (auths == null)
			return false;
		List authId = new ArrayList();
		for (; auths.size() > 0; auths.remove(0))
			authId.add(Integer.valueOf(Integer.parseInt(((String) auths.get(0))
					.trim())));

		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Authority.class);
		detachedCriteria.add(Restrictions.in("id", authId));
		List authsList = roleDao.findByCriteria(detachedCriteria);
		String modelNo = null;
		for (; authsList.size() > 0; authsList.remove(0)) {
			modelNo = ((Authority) authsList.get(0)).getAuthorityModuleNo();
			if (modelNo != null && modelNo.equals(String.valueOf(moduleId)))
				return true;
		}

		return false;
	}

	public List getRoleList() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Role.class);
		detachedCriteria.addOrder(Order.asc("roleSortId"));
		detachedCriteria.addOrder(Order.asc("roleName"));
		return roleDao.findByCriteria(detachedCriteria);
	}

	public List getRoleList(Integer roles[], String type) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Role.class);
		if (type.equals("roleSortId") || type.equals("roleNo"))
			detachedCriteria.add(Restrictions.in(type, roles));
		detachedCriteria.addOrder(Order.asc("roleSortId"));
		detachedCriteria.addOrder(Order.asc("roleName"));
		return roleDao.findByCriteria(detachedCriteria);
	}

	public boolean addRole(Role role) {
		if (role == null || role.getRoleName() == null
				|| role.getRoleName().equals("")) {
			return false;
		} else {
			roleDao.saveObject(role);
			return true;
		}
	}

	public String addRole(Role role, Integer authId[]) {
		String roleAuthority;
		Set authorities = new HashSet();
		Integer arr$[] = authId;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			Integer s = arr$[i$];
			authorities.add(String.valueOf(s));
		}

		Iterator itor = authorities.iterator();
		for (roleAuthority = ""; itor.hasNext(); roleAuthority = (new StringBuilder())
				.append(roleAuthority).append(itor.next()).append(",")
				.toString())
			;
		if (roleAuthority.length() == 0)
			return "noAuth";
		if (roleAuthority.length() > 255)
			return "authOver";
		int roleId;
		roleId = getSuitableNo().intValue();
		if (roleId > 99)
			return "roleOver";
		try {
			roleAuthority = roleAuthority.substring(0,
					roleAuthority.length() - 1);
			role.setRoleAuthority(roleAuthority);
			role.setRoleNo(roleId);
			roleDao.saveObject(role);
			return "yes";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "no";
	}

	public String delRole(Integer roleNo, UserBo userBo) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Role.class);
		detachedCriteria.add(Restrictions.eq("roleNo", roleNo));
		List roleList = roleDao.findByCriteria(detachedCriteria);
		if (roleList == null || roleList.size() != 1)
			return "ref";
		Role oldRole = (Role) roleList.get(0);
		DetachedCriteria detachedCriteria2 = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Userinfo.class);
		List userinfoList = roleDao.findByCriteria(detachedCriteria2);
		int userinfoSize = userinfoList.size();
		for (int i = 0; i < userinfoSize; i++) {
			Userinfo userinfo = (Userinfo) userinfoList.get(i);
			List userRoles = userinfo.getUiRoleList();
			if (userRoles != null && userRoles.contains(String.valueOf(roleNo)))
				return "err";
		}

		roleDao.deleteObject(oldRole);
		return "yes";
	}

	public String updateRole(Role role, Integer authId[], UserBo userBo) {
		Role oldRole;
		oldRole = getRole(role.getId());
		if (!oldRole.getRoleName().equals(role.getRoleName())) {
			Role roleTmp = roleDao.findRoleByName(role.getRoleName());
			if (roleTmp != null)
				return "exist";
		}
		String authority;
		oldRole.setRoleName(role.getRoleName());
		oldRole.setRoleDesc(role.getRoleDesc());
		oldRole.setRoleSortId(role.getRoleSortId());
		authority = StringUtil.authArrToString(authId);
		if (authority.length() == 0)
			return "noAuth";
		if (authority.length() > 255)
			return "authOver";
		List uiList;
		Integer users[];
		String newAuthType;
		IClientBO clientLimit;
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Userinfo.class);
		uiList = roleDao.findByCriteria(detachedCriteria);
		users = (new Integer[] { Integer.valueOf(0), Integer.valueOf(0),
				Integer.valueOf(0), Integer.valueOf(0) });
		if (uiList.size() > 0) {
			Iterator i$ = uiList.iterator();
			do {
				if (!i$.hasNext())
					break;
				Userinfo ui = (Userinfo) i$.next();
				if (ui.getUiStatus().intValue() != 0
						&& ui.getUiRoleList().contains(
								String.valueOf(oldRole.getRoleNo()))) {
					Integer ainteger[] = users;
					ainteger[3] = Integer.valueOf(ainteger[3].intValue() + 1);
					String authType = StringUtil.authStringToType(ui
							.getUiAuthDecrypt());
					if (authType == "USERADM") {
						Integer ainteger1[] = users;
						ainteger1[0] = Integer
								.valueOf(ainteger1[0].intValue() + 1);
					} else if (authType == "USERMGR") {
						Integer ainteger2[] = users;
						ainteger2[1] = Integer
								.valueOf(ainteger2[1].intValue() + 1);
					} else {
						Integer ainteger3[] = users;
						ainteger3[2] = Integer
								.valueOf(ainteger3[2].intValue() + 1);
					}
				}
			} while (true);
		}
		newAuthType = StringUtil.authStringToType(authority);
		clientLimit = (IClientBO) SpringBeanFactory.getBean("clientBo");
		if (newAuthType == "USERADM"
				&& users[1].intValue() + users[2].intValue() > 0) {
			String limit = clientLimit.checkUserLimit(newAuthType, users[1]
					.intValue()
					+ users[2].intValue());
			if (!"success".equals(limit))
				return "no";
		}
		if (newAuthType == "USERMGR" && users[2].intValue() > 0) {
			String limit = clientLimit.checkUserLimit(newAuthType, users[2]
					.intValue());
			if (!"success".equals(limit))
				return "no";
		}
		try {
			oldRole.setRoleAuthority(authority);
			roleDao.updateObject(oldRole);
			for (int i = 0; i < uiList.size(); i++) {
				Userinfo userinfo = (Userinfo) uiList.get(i);
				List userRoles = userinfo.getUiRoleList();
				if (userRoles.contains(String.valueOf(oldRole.getRoleNo()))) {
					String newAuths = userBo.getAuthoritysByRole(userinfo
							.getUiRoleDecrypt());
					userBo.updateUserAuth(userinfo, newAuths);
				}
			}

			return "yes";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "no";
	}

	public boolean findRoleByName(String roleName) {
		if (roleName == null || roleName.equals("")) {
			return false;
		} else {
			Role r = roleDao.findRoleByName(roleName);
			return r != null;
		}
	}

	public boolean findRoleBySortid(int sortId) {
		return roleDao.findRoleBySortid(sortId);
	}

	public List getAllAuthoritys(List list) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Authority.class);
		if (list != null)
			detachedCriteria.add(Restrictions.in("id", list));
		detachedCriteria.addOrder(Order.asc("authorityModuleNo"));
		detachedCriteria.addOrder(Order.desc("authorityConditionNo"));
		return roleDao.findByCriteria(detachedCriteria);
	}

	public Integer[] ListToInteger(List list) {
		int size = list.size();
		if (size < 1)
			return null;
		Integer integer[] = new Integer[size];
		String listContent = "";
		for (int i = 0; i < size; i++) {
			listContent = ((String) list.get(i)).trim();
			if (!listContent.equals(""))
				integer[i] = Integer.valueOf(Integer.parseInt(listContent));
		}

		return integer;
	}

	public Integer getSuitableNo() {
		for (int roleNo = 1; roleNo <= 99; roleNo++) {
			DetachedCriteria detachedCriteria = DetachedCriteria
					.forClass(com.wyzz.hr.pojo.Role.class);
			detachedCriteria.add(Restrictions.eq("roleNo", Integer
					.valueOf(roleNo)));
			int maxNoList = roleDao.findRowCountByCriteria(detachedCriteria);
			if (maxNoList <= 0)
				return Integer.valueOf(roleNo);
		}

		return Integer.valueOf(0x7fffffff);
	}

	private RoleDao roleDao;
}
